package cn.bigcontainer.alt.meta.util;

import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;

import cn.bigcontainer.alt.common.util.BigConstant;
import cn.bigcontainer.alt.common.util.WoKit;
import lombok.SneakyThrows;

public class MUtil {

    @SneakyThrows
    public static String getIds (List list, String getIdMethodName) {
        String ids = "";
        for (Object object : list) {
            if (!ids.isEmpty()) {
                ids += ",";
            }
            ids += object.getClass().getMethod(getIdMethodName).invoke(object);
        }
        return ids;
    }

    public static String getIds(List list) {
        try {
            return getIds(list, "getMId");
        } catch (Exception e) {
            return getIds(list, "getId");
        }
    }

    public static Long getPageSize () {
        return WoKit.getEnvPropertyLong("big.pagesize");
    }

    public static String createViewPropertyId(String viewId, String propName) {
        return (viewId + "_" + propName).toLowerCase();
    }
    
    /**
     * @param id
     * @return
     */
    public static String getFileUrl (Object id) {
		if (WoKit.isEmpty(id) || "0".equals(id)) {
			return null;
		}
		return "/meta/file/" + id;
	}
    
    /**
     * @param req
     * @return
     */
    @SneakyThrows
    public static Integer getCurrentUserId (Map<String, Object> req) {
    	if (req == null) {
    		return null;
    	}
        Object user = req.get(BigConstant.SESSION_USER);
        if (user == null) {
            return null;
        }
        Method getId = user.getClass().getMethod("getId");
        return (Integer) getId.invoke(user);
    }
}
